/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSRotate.cpp
 *
 *  Created on: 2010/03/05
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSRotate.h"
#include "RSSerializer.h"
#include <QXmlStreamWriter>
#include <math.h>

namespace Realsphere {

    RSRotate::RSRotate ( const QString& rScopeId ) :
        RSTransformationElement ( RS_ROTATE, rScopeId ), mAxis ( 1, 0, 0 )
    {
    }

    RSRotate::~RSRotate ()
    {
    }

    QVector3D RSRotate::Axis () const
    {
        return mAxis;
    }

    qreal RSRotate::Angle () const
    {
        return mAngle;
    }

    void RSRotate::SetAxis ( const QVector3D& rAxis, bool rEmit )
    {
        mAxis = rAxis.normalized ();
        mQuaternion = QQuaternion::fromAxisAndAngle ( rAxis, mAngle );
        mQuaternion.normalize ();

        SetDirty ( true );

        if ( rEmit )
            emit AxisChanged ( rAxis );
    }

    void RSRotate::SetAngle ( qreal rAngle, bool rEmit )
    {
        mAngle = rAngle;
        mQuaternion = QQuaternion::fromAxisAndAngle ( mAxis, rAngle );
        mQuaternion.normalize ();

        SetDirty ( true );

        if ( rEmit )
            emit AngleChanged ( rAngle );
    }

    void RSRotate::SetQuaternion ( const QQuaternion& rQuaternion, bool rEmit )
    {
        mQuaternion = rQuaternion.normalized ();
        mAxis = mQuaternion.vector ().normalized ();
        mAngle = ( acos ( mQuaternion.scalar () ) * 2.0 ) * RS_RAD_TO_DEG;

        SetDirty ( true );

        if ( rEmit )
        {
            emit AngleChanged ( mAngle );
            emit AxisChanged ( mAxis );
        }
    }

    void RSRotate::SetRotation ( const QVector3D& rAxis, qreal rAngle, bool rEmit )
    {
        QQuaternion q = QQuaternion::fromAxisAndAngle ( rAxis, rAngle );
        SetQuaternion ( q, rEmit );
    }

    void RSRotate::ApplyRotationR ( const QQuaternion& rQuaternion )
    {
        SetQuaternion ( mQuaternion * rQuaternion );
    }

    void RSRotate::ApplyRotationL ( const QQuaternion& rQuaternion )
    {
        SetQuaternion ( rQuaternion * mQuaternion );
    }

    void RSRotate::ApplyRotationR ( const QVector3D& rAxis, qreal rAngle )
    {
        QQuaternion q = QQuaternion::fromAxisAndAngle ( rAxis, rAngle );
        ApplyRotationR ( q );
    }

    void RSRotate::ApplyRotationL ( const QVector3D& rAxis, qreal rAngle )
    {
        QQuaternion q = QQuaternion::fromAxisAndAngle ( rAxis, rAngle );
        ApplyRotationL ( q );
    }

    void RSRotate::ApplyRotationR ( qreal rPitch, qreal rYaw, qreal rRoll )
    {
        QQuaternion qX = QQuaternion::fromAxisAndAngle ( RS_X_AXIS, rPitch );
        QQuaternion qY = QQuaternion::fromAxisAndAngle ( RS_Y_AXIS, rYaw );
        QQuaternion qZ = QQuaternion::fromAxisAndAngle ( RS_Z_AXIS, rRoll );
        ApplyRotationR ( qX * qY * qZ );
    }

    void RSRotate::ApplyRotationL ( qreal rPitch, qreal rYaw, qreal rRoll )
    {
        QQuaternion qX = QQuaternion::fromAxisAndAngle ( RS_X_AXIS, rPitch );
        QQuaternion qY = QQuaternion::fromAxisAndAngle ( RS_Y_AXIS, rYaw );
        QQuaternion qZ = QQuaternion::fromAxisAndAngle ( RS_Z_AXIS, rRoll );
        ApplyRotationL ( qX * qY * qZ );
    }

    void RSRotate::WriteElementData ( QXmlStreamWriter *rXmlWriter )
    {
        rXmlWriter->writeCharacters ( RSSerializer::serialize ( mAxis ) );
        rXmlWriter->writeCharacters ( " " );
        rXmlWriter->writeCharacters ( RSSerializer::serialize ( mAngle ) );
    }

    void RSRotate::UpdateTransformation ()
    {
        QMatrix4x4 transformation;
        transformation.rotate ( mQuaternion );
        SetTransformation ( transformation );
        SetDirty ( false );
    }
}
